package com.wy.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wy.common.R;

import com.wy.service.IDrugService;
import com.wy.entity.Drug;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 药品表 前端控制器
 * </p>
 *
 * @author 王昭贵
 * @since 2023-04-23
 */
@RestController
@RequestMapping("/drug")
public class DrugController {

    @Resource
    private IDrugService drugService;

    /****
     * 删除药物
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        Drug drug = drugService.getById(id);
        drug.setDeleteStatus(0);
        drugService.updateById(drug);
        return R.success("删除成功");
    }

    /****
     * 新增 更新药物
     * @param drug
     * @return
     */
    @PostMapping
    public R save(@RequestBody Drug drug) {
        drugService.saveOrUpdate(drug);
        return R.success("");
    }

    /****
     * 获取药品类型列表
     * @return
     */
    @GetMapping("/getDrugTypeList")
    public R getDrugTypeList() {
        List<Drug> list = drugService.list();

        List<String> stringList = list.stream().map(item -> item.getDrugType()).collect(Collectors.toList());
        HashSet<String> set = new HashSet<>(stringList);
        return R.success(set);
    }
    /****
     * 后台药物列表分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findPage(@RequestParam Integer page,
                      @RequestParam Integer pageSize,@RequestParam String name) {
        LambdaQueryWrapper<Drug> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(name)){
            queryWrapper.like(Drug::getDrugName,name);
        }
        queryWrapper.eq(Drug::getDeleteStatus,1).orderByDesc(Drug::getDrugId);
        return R.success(drugService.page(new Page<>(page, pageSize), queryWrapper));
    }

    /****
     * 获取药物列表
     * @return
     */
    @GetMapping
    public R findAll() {
        return R.success(drugService.list());
    }
    /****
     * 前台药物列表分页
     * @param page
     * @param pageSize
     * @param typeId
     * @return
     */
    @GetMapping("/FrontPage")
    public R findFrontPage(@RequestParam Integer page,
                                @RequestParam Integer pageSize,@RequestParam Integer typeId) {
        LambdaQueryWrapper<Drug> queryWrapper = new LambdaQueryWrapper<>();
        if(typeId==1){
            queryWrapper.eq(Drug::getDrugType,"感冒药");
        }else if(typeId==2){
            queryWrapper.eq(Drug::getDrugType,"抗生素");
        }else if(typeId==3){
            queryWrapper.eq(Drug::getDrugType,"维生素");
        }else if(typeId==4){
            queryWrapper.eq(Drug::getDrugType,"消化药");
        }else if(typeId==5){
            queryWrapper.eq(Drug::getDrugType,"心脏药");
        }else if(typeId==6){
            queryWrapper.eq(Drug::getDrugType,"止痛药");
        }
        queryWrapper.eq(Drug::getDeleteStatus,1).orderByDesc(Drug::getDrugId);
        return R.success(drugService.page(new Page<>(page, pageSize), queryWrapper));
    }

}

